/*
 * Copyright (C) 2010 The Android Open Source Project 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

package org.lhlzi.ithink;

import java.util.ArrayList;
import java.util.List;

import org.lhlzi.ithink.model.IRecentContactChangedListener;
import org.lhlzi.ithink.model.RecentContact;
import org.lhlzi.ithink.model.RecentEvent;

import android.content.Context;
import android.text.format.DateFormat;
import android.util.Log;

public class EventListBuilder {


	private static final String TAG = EventListBuilder.class.toString();

	// Temporary reference/variable
	private List<RecentContact> contacts;

	public EventListBuilder(List<RecentContact> previousContacts) {
		if (previousContacts != null) {
			this.contacts = previousContacts;
		} else {
			// May happen if the RWHolder was killed
			this.contacts = new ArrayList<RecentContact>();
		}
	}

	public void add(Context context, Long personId, String name, String number,
			RecentEvent newEvent, IRecentContactChangedListener listen) {

		// Unnecessary object creation?!?

		RecentContact contact = new RecentContact();
		contact.setNumber(number);
		contact.setPerson(name);
		contact.setPersonId(personId);
		CharSequence date = DateFormat.format("yyyy-MM-dd", newEvent.getDate());
		contact.setDate(date);
		

		// Fetch more information, so we can group events on a given contact and
		// so we have all the info to be displayed later on.

		contact = RecentWidgetUtils.CONTACTS_API.fetchContactInfo(context,
				contact);

		// i = index of the contact to be removed/added

		
		int i = 0;
		
		i = contacts.indexOf(contact);
		
		if (i >= 0) {
			contact = contacts.remove(i);

			// Contact removed might have less information than the info given
			// as parameters. Merge them.

			if (contact.getNumber() == null && number != null) {
				contact.setNumber(number);
			}
			if (contact.getPerson() == null && name != null) {
				contact.setPerson(name);
			}
			if (!contact.hasContactInfo() && personId != null) {
				contact.setPersonId(personId);
			}
		}
			
		
		

		// Add the event, if needed
		contact.addEvent(newEvent);

		// Put this contact at the right place

		// i = index where to add the new/updated contact

		i = contacts.size();

		for (int a = 0; a < i; a++) {
			if (contacts.get(a).getMostRecentDate() < contact
					.getMostRecentDate()) {
				i = a;
				break;
			}

		}

		
		contacts.add(i, contact);
		
		listen.onRecentContactChanged(i, contact);
		
	}


	public List<RecentContact> build() {
		// Just return the list...
		// Note: Do we fetch the picture and everything now?
		return contacts;
	}

	

}
